package io.coderyeah.org.controller;

import io.coderyeah.basic.Result;
import io.coderyeah.basic.exception.BusinessException;
import io.coderyeah.basic.util.ExcelUtils;
import io.coderyeah.org.domain.Shop;
import io.coderyeah.org.domain.ShopAuditLog;
import io.coderyeah.org.dto.ShopDTO;
import io.coderyeah.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api("店铺接口类")
@RequestMapping("/shop")
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;


    @ApiOperation("店铺入住")
    @PostMapping("/settlement")
    public Result settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
        } catch (BusinessException e) {
            // 自定义异常
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务器繁忙,请稍后重试！！！");
        }
        return Result.success("店铺入住成功");
    }

    @ApiOperation("获取所有店铺")
    @GetMapping
    public Result getAllShops() {
        return Result.success(shopService.list());
    }

    /**
     * 分页查询店铺列表
     *
     * @param dto 参数
     * @return result
     */
    @ApiOperation("店铺列表")
    @PostMapping()
    public Result getAll(@RequestBody ShopDTO dto) {
        return shopService.getAll(dto);
    }

    @ApiOperation("审核通过接口")
    @PostMapping("/audit/pass")
    public Result pass(@RequestBody ShopAuditLog auditLog) {
        try {
            return shopService.pass(auditLog);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail("店铺审核未通过！" + e.getMessage());
        } catch (Exception e) {
            return Result.fail("服务器异常，请稍后再试！！！");
        }
    }

    @ApiOperation("审核驳回接口")
    @PostMapping("/audit/reject")
    public Result reject(@RequestBody ShopAuditLog auditLog) {
        try {
            return shopService.reject(auditLog);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail("店铺审核驳回未通过！" + e.getMessage());
        } catch (Exception e) {
            return Result.fail("服务器异常，请稍后再试！！！");
        }
    }

    @ApiOperation("导出excel文件")
    @GetMapping("/exportExcel")
    public void export(HttpServletResponse response) {
        try {
            // 查询所有店铺信息
            final List<Shop> list = shopService.list();
            // List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response
            ExcelUtils.exportExcel(list, "店铺列表", "店铺数据", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("导入excel文件")
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        // 1 1分别是标题行数和头行数
        final List<Shop> shops = ExcelUtils.importExcel(file, 1, 1, Shop.class);
        System.out.println("导入数据共" + shops.size() + "行");
        shops.forEach(System.out::println);
        // 通过批量添加数据到数据库
//        shopService.saveBatch(shops, shops.size());
    }

    @ApiOperation("获取店铺echarts信息")
    @GetMapping("/echarts")
    public List<ShopDTO> getEchartsData() {
        return shopService.getEchartsData();
    }

    @ApiOperation("店铺激活")
    @GetMapping("/active/{shopId}")
    public Result active(@PathVariable("shopId") Long shopId) {
        return shopService.active(shopId);
    }
}

